package cpre556.project2.eclipsePlugin.support;

import java.util.Vector;

/**
 * 
 */
public class Comparator {
	/**
	 * 
	 */
	private Vector<String> toolsToCompare;
	
	/**
	 * 
	 * @param tools
	 */
	public Comparator(Vector<String> tools){
		toolsToCompare = tools;	
	}
	
	/**
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	private Vector<Vulnerability> getIntersection(Vector<Vulnerability> a, Vector<Vulnerability> b){
		Vector<Vulnerability> intersectionVulnerbilities = new Vector<Vulnerability>();
		
		int vTempSize = a.size();
		int vSize = b.size(); 
		
		for(int j = 0; j < vSize; j++){
			vSize = b.size();
			boolean found = false;
			int foundPosition = -1;
			
			for(int k = 0; k < vTempSize; k++){
				int compare1, compare2 = 0;
				
				compare1 = a.get(k).getLineNumber();
				compare2 = b.get(j).getLineNumber();
				
				if(compare1 == compare2){							
					found = true;							
					foundPosition = k;
					k = vTempSize;						
				}							
			}
			if(found){						
				Vulnerability tempA = new Vulnerability();
				tempA = a.get(foundPosition);
										
				tempA.setPriority("Intersection");					
				intersectionVulnerbilities.add(tempA);
			}					
		}			
		return intersectionVulnerbilities;
	}
	
	/**
	 * 
	 * @return
	 */
	public Vector<Vulnerability> getIntersectionVector(){
		Vector<Vulnerability> intersectionVulnerbilities = new Vector<Vulnerability>();
		Vector<Vulnerability> vulnerbilities = new Vector<Vulnerability>();
		Vector<Vulnerability> tempVulnerbilities = new Vector<Vulnerability>();
		
		if(toolsToCompare.size() > 0){
			XMLtoVulnerabilitiesVector xmlVector = new XMLtoVulnerabilitiesVector(toolsToCompare.get(0));		
			vulnerbilities = xmlVector.getVulnerabilities();
			
			XMLtoVulnerabilitiesVector xmlTempVector = new XMLtoVulnerabilitiesVector(toolsToCompare.get(1));
			tempVulnerbilities = xmlTempVector.getVulnerabilities();
			int vTempSize = tempVulnerbilities.size();
			int vSize = vulnerbilities.size(); 
			
			for(int j=0; j<vSize; j++){
				vSize = vulnerbilities.size();
				boolean found = false;
				int foundPosition = -1;
				
				for(int k = 0; k < vTempSize; k++){
					int compare1, compare2 = 0;
					
					compare1 = tempVulnerbilities.get(k).getLineNumber();
					compare2 = vulnerbilities.get(j).getLineNumber();
					
					if(compare1 == compare2){							
						found = true;							
						foundPosition = k;
						k = vTempSize;						
					}							
				}
				if(found){						
					Vulnerability tempNVulnerbility = new Vulnerability();
					tempNVulnerbility = tempVulnerbilities.get(foundPosition);
											
					tempNVulnerbility.setPriority("Intersection");					
					intersectionVulnerbilities.add(tempNVulnerbility);
				}					
			}
		}
		
		if(toolsToCompare.size() > 2){
			for(int i = 0; i < toolsToCompare.size(); i++){
				XMLtoVulnerabilitiesVector xmlVector = new XMLtoVulnerabilitiesVector(toolsToCompare.get(i));		
				Vector<Vulnerability> moreVulnerbilities = new Vector<Vulnerability>();
				moreVulnerbilities = xmlVector.getVulnerabilities();
				
				intersectionVulnerbilities = getIntersection(intersectionVulnerbilities, moreVulnerbilities);
			}
		}
		VulnerabilitySorter sorter = new VulnerabilitySorter();
		intersectionVulnerbilities = sorter.sortByLineNumber(intersectionVulnerbilities);
				
		return intersectionVulnerbilities;			
	}
	
	/**
	 * 
	 * @param a
	 * @return
	 */
	private Vector<Vulnerability> removeUnionDuplicates(Vector<Vulnerability> a){
		Vector<Vulnerability> unionVulnerbilities = new Vector<Vulnerability>();
		Vulnerability temp = new Vulnerability();
					
		for(int i = 0; i < a.size(); i++){
			if(i == 0){
				temp = a.get(i);
				temp.setPriority("Union");
				unionVulnerbilities.add(temp);
			}
			else{
				int compare1 = temp.getLineNumber();
				int compare2 = a.get(i).getLineNumber();
				String compare3 = temp.getFunctionName();
				String compare4 = a.get(i).getFunctionName();
				
				if(compare1 == compare2){
					if(!(compare3.equalsIgnoreCase(compare4)) || compare3.equalsIgnoreCase("")){
						a.get(i).setPriority("Union");
						unionVulnerbilities.add(a.get(i));
						temp = a.get(i);						
					}					
				}
				else{
					a.get(i).setPriority("Union");
					unionVulnerbilities.add(a.get(i));				
					temp = a.get(i);				
				}
			}			
		}
		
		VulnerabilitySorter sorter = new VulnerabilitySorter();
		unionVulnerbilities = sorter.sortByLineNumber(unionVulnerbilities);
		
		return unionVulnerbilities;		
	}	
	
	/**
	 * 
	 * @return
	 */
	public Vector<Vulnerability> getUnionVector(){
		Vector<Vulnerability> unionVulnerbilities  = new Vector<Vulnerability>();
		Vector<Vulnerability> union = new Vector<Vulnerability>();		
		
		if(toolsToCompare.size() > 0){
			XMLtoVulnerabilitiesVector xmlVector = new XMLtoVulnerabilitiesVector(toolsToCompare.get(0));		
			union = xmlVector.getVulnerabilities();
			
			for(int i = 1; i< toolsToCompare.size(); i++){
				Vector<Vulnerability> tempUnion = new Vector<Vulnerability>();
				XMLtoVulnerabilitiesVector xmlTempVector = new XMLtoVulnerabilitiesVector(toolsToCompare.get(i));		
				tempUnion = xmlTempVector.getVulnerabilities();
				
				for(int j = 0; j < tempUnion.size(); j++){
					union.add(tempUnion.get(j));
				}				
			}
			
			VulnerabilitySorter sorter = new VulnerabilitySorter();
			union = sorter.sortByLineNumber(union);
		}
		
		unionVulnerbilities = removeUnionDuplicates(union);
		return unionVulnerbilities;
		
	}
}
